package com.zztl.infiniteBand.ui.activity

import android.support.v7.widget.LinearLayoutManager
import com.zztl.infiniteBand.BraceletApplication
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.adapter.BindedDeviceAdapter
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.eventbus.BindEvent
import com.zztl.infiniteBand.model.BindedDeviceBean
import com.zztl.infiniteBand.presenter.MyDevicePresenter
import com.zztl.infiniteBand.ui.view.mvpview.UnbindView
import com.zztl.infiniteBand.ui.view.recycleview.SimpleItemDecoration
import com.zztl.infiniteBand.util.toast
import kotlinx.android.synthetic.main.activity_my_device.*
import org.greenrobot.eventbus.EventBus


class MyDeviceActivity : BaseMvpActivity<UnbindView, MyDevicePresenter>(), UnbindView {


    private val watchList: ArrayList<BindedDeviceBean.DataBean.RowsBean> by lazy {
        arrayListOf<BindedDeviceBean.DataBean.RowsBean>()
    }
    lateinit var mDeviceAdapter: BindedDeviceAdapter
    lateinit var mDevicePresenter: MyDevicePresenter



    override fun setLayoutId(): Int = R.layout.activity_my_device


    override fun initBase() {
        setTitle(getString(R.string.设备绑定))
        mDeviceAdapter = BindedDeviceAdapter(R.layout.item_binded_device, watchList!!)
        LinearLayoutManager(this).let {
            it.orientation = LinearLayoutManager.VERTICAL
            recycleView.layoutManager = it
        }
        recycleView.addItemDecoration(SimpleItemDecoration(this))
        recycleView.adapter = mDeviceAdapter
        mDevicePresenter.getBindDevice(this)
    }

    /* private fun showDisconnectDialog(position: Int) {
         if (mExitDialog == null) {
             mExitDialog = ExitDialog.Builder(this).setMessage(getString(R.string.请先断开蓝牙连接)).setClickListener(object : ExitDialog.Builder.ClickListener {
                 override fun cancel() {}

                 override fun comfirm() {
                     BraceletApplication.zhBraceletService?.disconnectDevice()
                     mDevicePresenter.unBindDevice(watchList[position].imei, this@MyDeviceActivity)
                 }
             }).create()
         }
         mExitDialog!!.show()
     }*/

    override fun createPresenter(): MyDevicePresenter {
        mDevicePresenter = MyDevicePresenter()
        return mDevicePresenter
    }


    override fun unBindSuccess() {
        BraceletApplication.getmDaoSession()?.bindedWatchBeanDao?.deleteAll()
        watchList.clear()
        mDeviceAdapter.notifyDataSetChanged()
        EventBus.getDefault().post(BindEvent(false))
//        ToastUtils.show(this, getString(R.string.解绑成功))
        toast(getString(R.string.解绑成功))
    }

    override fun unBindFail() {

    }

    override fun getBindDeviceList(bean: BindedDeviceBean) {
       bean.data?.rows?.let {
            for (i in it.indices) {
                if (!it[i].type.equals("0")) {
                    watchList.add(it[i])
                }
            }
        }
        mDeviceAdapter.notifyDataSetChanged()
    }
}
